package com.maserb.monitor.web.jw_our;


import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwEducationClobEntity;
import com.maserb.monitor.entity.jwOur.JwOurEntity;
import com.maserb.monitor.enums.JwOurTypeEnum;
import com.maserb.monitor.service.jwOur.JwOurService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

//关于我们
@Controller
@RequestMapping("/jwOur")
public class JwOurController {
    @Autowired
    private JwOurService service;

    @RequestMapping(value = {"/", "/index"})
    public String getIndex() {
        return "jw_our/index";
    }

    @RequestMapping("/doListOur")
    @ResponseBody
    public PageResponseResult<JwOurEntity> doListUser(PageRequestParam page) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<JwOurEntity>>() {
            @Override
            public Page<JwOurEntity> invoke(Pageable var1) throws Exception {
                Page<JwOurEntity> page = service.findAll(var1);
                for(JwOurEntity entity:page.getContent()){
                    if(null!=entity.getType() && !"".equals(entity.getType())){
                        entity.setTypeCn(JwOurTypeEnum.getNameByValue(Integer.parseInt(entity.getType())));
                    }
                }
                return page;
            }
        });
    }


    @RequestMapping("/getAddOur")
    public ModelAndView getAddOur(){
        ModelAndView view = new ModelAndView();
        view.setViewName("jw_our/our_add");
        JwOurTypeEnum[] infoType = JwOurTypeEnum.values();
        view.addObject("infoType",infoType);
        return view;
    }

    @RequestMapping("/doAddOur")
    @ResponseBody
    public AjaxResult<Boolean> doAddOur(JwOurEntity entity,String clobValues) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                entity.setCreateTime(CreateTimeUtils.getCreateTime());
                saveOur(entity, clobValues);
                return true;
            }
        });
    }

    @RequestMapping("/getUpdateOur/{id}")
    public ModelAndView getUpdateOur(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        JwOurEntity entity = service.findOne(id);
        result.setViewName("jw_our/our_edit");
        JwOurTypeEnum[] infoType = JwOurTypeEnum.values();
        result.addObject("infoType",infoType);
        result.addObject("entity", entity);
        return result;
    }

    @RequestMapping("/doUpdateOur")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateOur(final HttpServletRequest request, JwOurEntity entity, String clobValues) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                saveOur(entity, clobValues);
                return true;
            }
        });
    }

    @RequestMapping("/doDeleteOur")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteOur(@RequestParam(value = "keys[]") ArrayList<String> keys)
    {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                service.deleteAllByKeys(keys);
                return true;
            }
        });
    }

    private void saveOur(JwOurEntity entity,String clobValues){
        JwEducationClobEntity clobEntity = new JwEducationClobEntity();
        clobEntity.setInfoValue(clobValues);
        entity.setClob(clobEntity);
        service.save(entity,clobEntity);
    }
}
